<template>
  <div class="container">
    <div class="headers">
      <div>作业管理</div>
      <el-button type="primary" @click="handleedit('','add')">新增作业</el-button>
    </div>
    <!-- 表单区域 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="taskTitle" label="作业名称" align="center"></el-table-column>
      <el-table-column prop="taskDescription" label="作业描述" align="center"></el-table-column>
      <el-table-column prop="className" label="所属班级" align="center"></el-table-column>
      <el-table-column prop="taskType" label="作业类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.taskType == 1">上传类型</span>
          <span v-if="scope.row.taskType == 2">问卷类型</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleedit(scope.row,'edit')">编辑</el-button>
          <el-button type="text" @click="handlelook(scope.row,'look')">查看提交情况</el-button>
          <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination" v-if="totalArr > 10">
      <el-pagination background
        @current-change="handleCurrentChange"
        :current-page="parseInt(queryParam.pageNum)"
        layout="prev, pager, next,total"
        :total="totalArr">
      </el-pagination>
    </div>
    <!--作业操作-->
    <edit-work ref="editWork" @refreshData="getData"></edit-work>
    <!-- 作业提交情况 -->
    <stats-work ref="statsWork" @refreshData="getData"></stats-work>
  </div>
</template>

<script>
import editWork from './editwork.vue';
import statsWork from './workstats.vue';
import {delHomework, getHomeworkList} from "../../api/apiz";
export default {
  name: '',
  components: {editWork,statsWork},
  data() {
    return {
      classId: '',
      queryParam: {
        pageNum: 1,
        pageSize: 10,
      },
      totalArr:0,
      tableData: [],
    }
  },
  methods: {
    getHomeworkData(data){
      this.classId = data
      this.getData();
    },
    getData(){
      this.queryParam.classId = this.classId
      getHomeworkList(this.queryParam).then(res => {
        if (res.code == 200){
          this.tableData = res.result.records
          this.totalArr = res.result.total
        }else {
          this.$message.warning(res.message)
        }
      })
    },
    // 新增编辑
    handleedit(data,type) {
      this.$nextTick(() => {
        this.$refs.editWork.getWorkData(data,type,this.classId);
      });
    },
    // 查看提交情况
    handlelook(data) {
      this.$nextTick(() => {
        this.$refs.statsWork.getWorkData(data);
      });
    },
    handleCurrentChange(val){
      this.queryParam.pageNum = val;
      this.getData();
    },
    handleDelete(id){
      this.$confirms('此操作将删除该作业, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id: id
        }
        console.log(params)
        delHomework(params).then(res => {
          if (res.code == 200){
            this.$message.success('删除成功');
            this.getData();
          }else {
            this.$message.warning(res.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>
<style scoped lang='less'>
.container {
  padding-right: 20px;
}

.headers {
  display: flex;
  justify-content: space-between;
  padding: 15px 0;
}
.pagination{
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
